<?php
class ControllerLocalisationReturnStatus extends AdminController {

  	public function index() {
    	M('localisation/return_status');
    	$this->getList();
  	}

  	public function insert() {
		M('localisation/return_status');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_localisation_return_status->addReturnStatus($this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/return_status'));
		}

    	$this->getForm();
  	}

  	public function update() {
		M('localisation/return_status');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_localisation_return_status->editReturnStatus($this->request->get['return_status_id'], $this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/return_status'));
    	}

    	$this->getForm();
  	}

  	public function delete() {
		M('localisation/return_status');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $return_status_id) {
				$this->model_localisation_return_status->deleteReturnStatus($return_status_id);
			}
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/return_status'));
   		}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();
		$return_status_total = $this->model_localisation_return_status->getTotalReturnStatuses();
		$this->data['return_statuses'] = $this->model_localisation_return_status->getReturnStatuses($qf);

		$this->data['pagination'] = $this->pagination($return_status_total)->render();

		$this->display('localisation/return_status_list.tpl');
  	}

  	protected function getForm() {
     	$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['return_status_id'])) {
			$this->data['action'] = UA('localisation/return_status/update', 'return_status_id=' . $this->request->get['return_status_id']);
			
		} else {
			$this->data['action'] = UA('localisation/return_status/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		if (isset($this->request->post['return_status'])) {
			$this->data['return_status'] = $this->request->post['return_status'];
		} elseif (isset($this->request->get['return_status_id'])) {
			$this->data['return_status'] = $this->model_localisation_return_status->getReturnStatusDescriptions($this->request->get['return_status_id']);
		} else {
			$this->data['return_status'] = array();
		}

		$this->display('localisation/return_status_form.tpl');
  	}

	protected function validateForm() {

		$pass = true;

    	foreach ($this->request->post['return_status'] as $language_id => $value) {
      		if (!range_length($value['name'], 3, 32)) {
        		$this->setMessage('error_name_' . $language_id, $this->_['error_name']);
				$pass = false;
      		}
    	}

		return $pass;
  	}

  	protected function validateDelete() {

		M('sale/return');

		foreach ($this->request->post['selected'] as $return_status_id) {
    		if (C('config_return_status_id') == $return_status_id) {
	  			$this->setMessage('error_warning', $this->_['error_default']);
				return false;
			}

			$return_total = $this->model_sale_return->getTotalReturnsByReturnStatusId($return_status_id);

			if ($return_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_return'], $return_total));
				return false;
			}

			$return_total = $this->model_sale_return->getTotalReturnHistoriesByReturnStatusId($return_status_id);

			if ($return_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_return'], $return_total));
				return false;
			}
	  	}

		return true;
  	}
}
?>